package com.qf.web;

import cn.hutool.core.util.IdUtil;
import com.qf.model.User;
import com.qf.model.UserCard;
import com.qf.service.UserService;
import com.qf.service.impl.UserServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

@WebServlet("/accountServlet")
public class AccountServlet extends BaseServlet{
    private UserService userService = new UserServiceImpl();
    public void onAccount(HttpServletRequest req, HttpServletResponse resp){
        try {
            String name = req.getParameter("name");
            String idnumber = req.getParameter("idnumber");
            String phone = req.getParameter("phone");
            User user1 = userService.findUser(phone);
            if(user1!=null){
                user1.setName(name);
                user1.setIdnumber(idnumber);
                int row = userService.update(user1);
                if(row>0){
                    req.getSession().setAttribute("user",user1);
                    req.getRequestDispatcher("/account/setpwd.jsp").forward(req,resp);
                }else {
                    req.getSession().setAttribute("user",user1);
                    req.getRequestDispatcher("/account/account.jsp").forward(req,resp);
                }
            }

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public void setPayPassword(HttpServletRequest req,HttpServletResponse resp) throws IOException {
        User user = (User) req.getSession().getAttribute("user");
        String payPassword = req.getParameter("payPassword");
        int uid = user.getUid();
        String cid = IdUtil.getSnowflakeNextIdStr();
        int row = userService.setPayPassword(payPassword,uid,cid);
        if(row>0){
            resp.sendRedirect("/account/alert_success.jsp");
        }else {
            req.setAttribute("msg","服务器阵亡");
            req.getRequestDispatcher("/error/error.jsp");
        }
    }

    public void onCard(HttpServletRequest req,HttpServletResponse resp) throws IOException {
        User user = (User) req.getSession().getAttribute("user");
        String cardnum = req.getParameter("cardnum");
        String onCardPassword = req.getParameter("onCardPassword");
        UserCard onCard = userService.findOnCard(cardnum,onCardPassword);
        if (onCard!=null) {
            onCard.setUid(user.getUid());
            if (onCard != null) {
                int row = userService.addUserCard(onCard);
                if (row > 0) {
                    List<UserCard> usercads =  userService.findAllCard(user);//查询银行卡列表，将该用户银行卡存储
                    user.setUserCards(usercads);
                    BigDecimal allCardMoney = userService.findAllCardMoney(usercads);  //查询用户所有银行卡余额
                    BigDecimal allMoney = new BigDecimal(0);
                    allMoney = allCardMoney.add(user.getMoney());
                    req.getSession().setAttribute( "allMoney",allMoney);
                    req.getSession().setAttribute("user",user);
                    resp.sendRedirect("/homepage/home_page.jsp");

                } else {
                    req.setAttribute("msg", "绑卡失败");
                    resp.sendRedirect("/error/oncard_error.jsp");
                }
            }
        }else {
            req.setAttribute("msg","系统正忙，请稍后重试");
            req.getRequestDispatcher("/error/oncard_error.jsp");
        }
    }

}
